package com.zhiyou100.review.day019.homework;

/**
 * @packageName: Homework1
 * @className: com.zhiyou100.review.day019.homework
 * @Description: TODO june 30,2020 作业实现三个线程 分别打印数字 、大写、小写 打印大写的线程 打印到M时等待小写线程 打印小写的线程 打印到t时等待数字线程
 * @author: yang
 * @date: 6/30/20
 */
public class Homework4 {
    public static void main(String[] args) {
        PrintChar number = new PrintChar("数字", '0', '9', '0');
        PrintChar capital = new PrintChar("大写", 'A', 'Z', 'M');
        PrintChar lowercase = new PrintChar("小写字母", 'a', 'z', 't');

        capital.j = lowercase;
        lowercase.j = number;

        //HomeWorkOfMath.randomOfMinToMax()

        number.start();
        capital.start();
        lowercase.start();


    }
}


class PrintChar extends Thread {
    public PrintChar(String name, int start, int end, char flag) {
        super(name);
        this.start = start;
        this.end = end;
        this.flag = flag;
    }

    public PrintChar(String name, int start, int end) {
        super(name);
        this.start = start;
        this.end = end;
    }


    Thread j;
    // 要等待的线程
    int start;
    int end;
    // 开始结束的字符
    char flag;
    // 停止的标记

    // flag 记录该线程等待其他线程的条件
    @Override
    public void run() {

        for (int i = 0; i < 200; i++) {
            char c = new PrintMethod().printOne(start, end);
            System.out.println(Thread.currentThread().getName() + " 打印了 " + c);
            try {
                Thread.sleep(100);
                // 休眠一会
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (c == flag && j != null) {
                // 防止 空指针异常
                try {
                    j.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
